package com.example.boxugu.myapplication.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.boxugu.myapplication.Moder.Course_bannerModer;
import com.example.boxugu.myapplication.Moder.PractisedetailModer;
import com.example.boxugu.myapplication.Moder.VediodetailModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

public class Coursedetail_Activity extends AppCompatActivity {
    String id;
    TextView accunttextView;
    TextView vediotextView;
    RecyclerView recyclerView;
    TextView AccountMessage;
    MyAdapter myAdapter;
    ArrayList<VediodetailModer.VideoList>videoLists=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coursedetail_);
        if (getIntent()!= null &&getIntent().getStringExtra("id")!=null){
            id=getIntent().getStringExtra("id");
        }
        accunttextView=findViewById(R.id.account);
        vediotextView=findViewById(R.id.vedio);
        recyclerView=findViewById(R.id.VedioMessage);
        AccountMessage=findViewById(R.id.AccountMessage);
        myAdapter=new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(Coursedetail_Activity.this));

        accunttextView.setSelected(true);
        vediotextView.setSelected(false);
        AccountMessage.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
        accunttextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            accunttextView.setSelected(true);
            vediotextView.setSelected(false);
                AccountMessage.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
        });
        vediotextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                accunttextView.setSelected(false);
                vediotextView.setSelected(true);
                AccountMessage.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
            }
        });
        RequestParams params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/chapterdetail");
        params.addBodyParameter("id",id);
        x.http().post(params, new Callback.CacheCallback<String>() {
            @SuppressLint("ResourceType")
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                VediodetailModer vediodetailModer = gson.fromJson(result, VediodetailModer.class);
                if (vediodetailModer!=null&&vediodetailModer.data!=null){
                  videoLists=vediodetailModer.data.videoList;
                  AccountMessage.setText(vediodetailModer.data.des);
                  myAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {


            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{


        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view=View.inflate(Coursedetail_Activity.this,R.layout.video_item,null);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
            myViewHolder.vediotitle.setText(videoLists.get(i).videotitle);
            myViewHolder.mlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myViewHolder.imageView.setSelected(true);
                    Intent intent=new Intent(Coursedetail_Activity.this,VideoActivity.class);
                    intent.putExtra("videourl",videoLists.get(i).videourl);
                    startActivity(intent);
                }

            });


        }


        @Override
        public int getItemCount() {
            return videoLists.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
         TextView vediotitle;
         ImageView imageView;
         LinearLayout mlayout;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            vediotitle=itemView.findViewById(R.id.videotitle);
            imageView=itemView.findViewById(R.id.vedio_img);
            mlayout=itemView.findViewById(R.id.videolayout);
        }
    }
}
